import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { DataEvent } from "../../../events/DataEvent";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { LGXBDirection } from "../dir/LGXBDirection";
import { MathUitl } from "../../../../../com/gengine/utils/MathUitl";
type int = number;
//class ZhuanPanViewerObject
    
    export  class ZhuanPanViewerObject /* flash.display.Sprite */
    {
        constructor(arg1: Object /* flash.display.Sprite */)
        {
            
            this.__parent = arg1;
            this._timer = new FrameTimer();
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandlerNormal);
            this._timer.repeatCount = Number.MAX_VALUE;
            return;
        }

        /* internal  */onEnterFrameHandlerFast(arg1: FrameTimer): void
        {
            var loc1=NaN;
            if (this.toAddSpeed) 
            {
                if (this.curSpeed < 60) 
                {
                    this.curSpeed = this.curSpeed + 3;
                }
                if (this.curSpeed >= 60) 
                {
                    this.toAddSpeed = false;
                    loc1 = (this.pointer.rotation + 360) % 360;
                    if (this.targetAngle < loc1) 
                    {
                        this.targetAngle = this.targetAngle + 360;
                    }
                    else if (this.targetAngle - loc1 < 270) 
                    {
                        this.targetAngle = this.targetAngle + 360;
                    }
                    this.offAngle = 28;
                    this.targetAngle = this.targetAngle + this.offAngle;
                    this.SpeedA = this.curSpeed * this.curSpeed / 2 / (this.targetAngle - loc1);
                    return;
                }
            }
            else if (this.curSpeed >= this.SpeedA) 
            {
                this.curSpeed = this.curSpeed - this.SpeedA;
            }
            else 
            {
                this._timer.stop();
                this.curRotateNum = 0;
                flash.utils.setTimeout(this.delayToCall, 500);
            }
            this.pointer.rotation = this.pointer.rotation + this.curSpeed;
            return;
        }

        /* internal  */onEnterFrameHandlerNormal(arg1: FrameTimer): void
        {
            if (this.toAddSpeed) 
            {
                if (this.curSpeed < 3) 
                {
                    this.curSpeed = this.curSpeed + 0.1;
                }
                else if (this.curSpeed < 10) 
                {
                    this.curSpeed = this.curSpeed + 0.4;
                }
                else if (this.curSpeed < 60) 
                {
                    this.curSpeed = this.curSpeed + 1;
                }
                if (this.curSpeed >= 60) 
                {
                    this.curSpeed = 60;
                    var loc1;
                    var loc2=((loc1 = this).curRotateNum + 1);
                    loc1.curRotateNum = loc2;
                    if (this.curRotateNum >= 10) 
                    {
                        this.toAddSpeed = false;
                        this.offAngle = (60 * 60 - 10 * 10) / (2 * 1) + (10 * 10 - 4 * 4) / (2 * 0.3) + (4 * 4 - 0) / (2 * 0.05);
                        this.offAngle = 360 - int(this.offAngle) % 360;
                        this.pointer.rotation = this.offAngle + this.targetAngle;
                    }
                }
            }
            else if (this.curSpeed >= 10) 
            {
                --this.curSpeed;
            }
            else if (this.curSpeed >= 4) 
            {
                this.curSpeed = this.curSpeed - 0.3;
            }
            else if (this.curSpeed > 0) 
            {
                this.curSpeed = this.curSpeed - 0.05;
            }
            else 
            {
                this._timer.stop();
                this.curRotateNum = 0;
                flash.utils.setTimeout(this.delayToCall, 500);
            }
            this.pointer.rotation = this.pointer.rotation + this.curSpeed;
            return;
        }

        /* internal  */delayToCall(): void
        {
            this.dispatchEvent(new DataEvent("#stopRotate#", this.targetSide));
            return;
        }

        /* internal  */initView(): void
        {
            var loc1=null;
            this.hasInit = true;
            this.bg = ObjCreate.createBitmap(ImagesConst.LGXB_zhuanPan, 0, 0, this);
            this.directBox = new Object /* flash.display.Sprite */();
            this.addChild(this.directBox);
            this.pointer = new Object /* flash.display.Sprite */();
            loc1 = ObjCreate.createBitmap(ImagesConst.LGXB_pointer, -23, -72, this.pointer);
            loc1.smoothing = true;
            this.addChild(this.pointer);
            this.pointer.x = 79;
            this.pointer.y = 98;
            return;
        }

        /* internal  */addDirectIcons(arg1: Array<any>): void
        {
            while (this.directBox.numChildren > 0) 
            {
                this.directBox.removeChildAt(0);
            }
            if (arg1.length != 3) 
            {
                this.deelFourRoads(arg1);
            }
            else 
            {
                this.deelThreeRoads(arg1);
            }
            return;
        }

        /* internal  */deelFourRoads(arg1: Array<any>): void
        {
            var loc2=0;
            var loc1=LGXBDirection.instance.getDirectonIcon(arg1[0], false);
            ObjCreate.setObjAttri(loc1, 15, 85, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[1], false);
            ObjCreate.setObjAttri(loc1, 33, 37, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[2], false);
            ObjCreate.setObjAttri(loc1, 82, 37, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[3], true);
            ObjCreate.setObjAttri(loc1, 100, 85, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[1], true);
            ObjCreate.setObjAttri(loc1, 56, 116, -1, -1, this.directBox);
            if (this.targetSide != arg1[0]) 
            {
                if (this.targetSide != arg1[1]) 
                {
                    if (this.targetSide != arg1[2]) 
                    {
                        if (this.targetSide != arg1[3]) 
                        {
                            throw new Error("方向错误");
                        }
                        else 
                        {
                            this.targetAngle = MathUitl.random(74, 138);
                        }
                    }
                    else 
                    {
                        this.targetAngle = MathUitl.random(4, 65);
                    }
                }
                else 
                {
                    loc2 = MathUitl.random(0, 1);
                    if (loc2 != 0) 
                    {
                        this.targetAngle = MathUitl.random(148, 215);
                    }
                    else 
                    {
                        this.targetAngle = MathUitl.random(295, 355);
                    }
                }
            }
            else 
            {
                this.targetAngle = MathUitl.random(225, 285);
            }
            return;
        }

        /* internal  */deelThreeRoads(arg1: Array<any>): void
        {
            var loc1=LGXBDirection.instance.getDirectonIcon(arg1[0], false);
            ObjCreate.setObjAttri(loc1, 15, 85, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[1], false);
            ObjCreate.setObjAttri(loc1, 33, 37, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[1], true);
            ObjCreate.setObjAttri(loc1, 100, 85, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[2], false);
            ObjCreate.setObjAttri(loc1, 82, 37, -1, -1, this.directBox);
            loc1 = LGXBDirection.instance.getDirectonIcon(arg1[2], true);
            ObjCreate.setObjAttri(loc1, 56, 116, -1, -1, this.directBox);
            var loc2=MathUitl.random(0, 1);
            if (this.targetSide != arg1[0]) 
            {
                if (this.targetSide != arg1[1]) 
                {
                    if (this.targetSide != arg1[2]) 
                    {
                        throw new Error("方向错误");
                    }
                    else if (loc2 != 0) 
                    {
                        this.targetAngle = MathUitl.random(148, 215);
                    }
                    else 
                    {
                        this.targetAngle = MathUitl.random(4, 65);
                    }
                }
                else if (loc2 != 0) 
                {
                    this.targetAngle = MathUitl.random(74, 138);
                }
                else 
                {
                    this.targetAngle = MathUitl.random(295, 355);
                }
            }
            else 
            {
                this.targetAngle = MathUitl.random(225, 285);
            }
            return;
        }

        public set jumpAnimate(arg1: Boolean)
        {
            if (this._isJumpAnimate == arg1) 
            {
                return;
            }
            this._isJumpAnimate = arg1;
            if (arg1) 
            {
                if (this.isShowing) 
                {
                    this.jumpAnimateHander();
                }
            }
            return;
        }

        /* internal  */jumpAnimateHander(): void
        {
            this.clear();
            this.hide();
            return;
        }

        public show(arg1: Array<any>, arg2: int): void
        {
            if (this._isJumpAnimate) 
            {
                this.jumpAnimateHander();
                return;
            }
            if (!this.hasInit) 
            {
                this.initView();
            }
            this.targetSide = arg2;
            this.addDirectIcons(arg1);
            this.__parent.addChild(this);
            this.x = 280;
            this.y = 100;
            this.clear();
            this.timerid = flash.utils.setTimeout(this.reStartTimer, 300);
            this.isShowing = true;
            return;
        }

        /* internal  */clear(): void
        {
            this._timer.stop();
            if (this.pointer) 
            {
                this.pointer.rotation = 0;
            }
            flash.utils.clearTimeout(this.timerid);
            return;
        }

        /* internal  */reStartTimer(): void
        {
            this.toAddSpeed = true;
            this.curSpeed = 0;
            this.curRotateNum = 0;
            this._timer.start();
            return;
        }

        public hide(): void
        {
            if (this.parent) 
            {
                this.parent.removeChild(this);
            }
            this.isShowing = false;
            return;
        }

        /* internal */ /* var */pointer: Object /* flash.display.Sprite */;

        /* internal */ /* var */bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */directBox: Object /* flash.display.Sprite */;

        /* internal */ /* var */__parent: Object /* flash.display.Sprite */;

        /* internal */ /* var */hasInit: Boolean=false;

        /* internal */ /* var */_timer: FrameTimer;

        /* internal */ /* var */curSpeed: number=0;

        /* internal */ /* var */SpeedA: number=5;

        /* internal */ /* var */toAddSpeed: Boolean;

        /* internal */ /* var */targetSide: int;

        /* internal */ /* var */curRotateNum: int=0;

        /* internal */ /* var */targetAngle: number;

        /* internal */ /* var */offAngle: int=40;

        /* internal */ /* var */timerid: int=-1;

        /* internal */ /* var */_isJumpAnimate: Boolean=false;

        public /* var */isShowing: Boolean=false;
    }
